function H = localhists(M, K, psiz, intvs)
%LOCALHISTS Computes Map of Local histograms
%
%   H = localhists(M, K, psiz, intvs);
%
%       Computes a map of local histograms from a map of words.
%
%       Input arguments:
%       - M:        The input image of word maps
%       - K:        The vocabulary size
%       - psiz:     The patch size [ph, pw]
%       - intvs:    The intervals [yintv, xintv]
%
%       Output argument:
%       - H:        The map of histograms: K x h2 x w2
%                   Here, h2 = h - lh + 1, w2 = w - lw + 1.
%

% Created by Dahua Lin, on Mar 8, 2012
%

%% verify input arguments

if ~(isnumeric(M) && isreal(M) && ~issparse(M) && ndims(M) == 2)
    error('localhists:invalidarg', 'M should be a two-dimensional matrix.');
end

if ~(isnumeric(K) && isscalar(K) && K == fix(K) && K >= 1)
    error('localhists:invalidarg', 'K should be a positive integer.');
end

%% main

P = patch2vecs(M, psiz, intvs);
P = P(:, :);

H = intcount(K, P, 1);
H = bsxfun(@times, H, 1./sum(H,1));

